/*
 * Copyright (C) 2018-2020 Tsing Micro Holding Limited
 */

#include "wrapper.h"
#include "drv_isp.h"
#include "aos/hal/i2c.h"
#include "lv_port_disp.h"
#include "lv_conf.h"
#include "drv_dpu.h"
#include "dpu.h"
#include "debug_time.h"
// #include "kv_api.h"
// #include "kv_adapt.h"
//#include "doordata.h"
// #include "kv_types.h"
#include "para_traverse_task.h"
// #include <fcntl.h>
#include "stdint.h"
#include "vfs.h"
#include "if_v.h"

#include "board_sensor.h"
#include "iic_function.h"
#include "isp_function.h"
#include "cam_engine_ctrl.h"
#include "irp_jp.h"
#include "drv_lcd_st7789.h"
#include "mipi_function.h"
// #include "kv_adapt.h"
// #include "kv_types.h"

extern int sensor_reconfig(uint8_t mipi_ID, uint8_t mipiWorkMode_Flag);
extern void qw_isp_ae_lock_judge_set(uint32_t isp_id, bool flag);
extern void qw_isp_ae_enable_set(uint32_t isp_id, bool flag);
extern void isp_reset(int32_t idx);
extern int isp_unInit(int isp_id);

extern int g_scenario_type;
extern data_flow_func_control_t g_app_flow_cfg;

dpu_handle_t dpu_handle;
i2c_dev_t i2c_devices[CONFIG_IIC_NUM] = {0};
uint8_t mipiWorkModeFlag;
/* mipiWorkStatusFlag explain:
   bit 0��is on IR reg/recog;  1 on IR ; 0 not on
   bit 1: is could change mipi work mode; 1 could; 0 couldn't
   bit 2: need to change to RGB work mode;1 need ; 0 don't need
   bit 3: need to change to IR work mode; 1 need ; 0 don't need
   bit 4: is on rgb mode; 1 on; 0 not on
   */
uint8_t mipiWorkStatusFlag;
aos_mutex_t mipiWorkStatusMutex;

/*  
 *  Video input initialization function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
int tsm_vi_start(TSM_VI_INFO_S *pstViInfo)
{
    i2c_dev_t       i2c_config;
	imageChannel_t  iChn;
    int             ret = 0;

    /* 1. Initialize I2C connected sensor */
    memset(&i2c_config,0,sizeof(i2c_config));
	i2c_config.port = pstViInfo->stSnsInfo.u32PortId;
	i2c_config.config.dev_addr = pstViInfo->stSnsInfo.u32BusId;
    i2c_config.universal = 1;	//for fullmask
	i2c_config.i2cNo = pstViInfo->stSnsInfo.u32SnsId;
	i2c_config.config.freq = I2C_BUS_BIT_RATES_400K;
	ret = hal_i2c_init(&i2c_config);
	if (ret < 0) 
    {
		printf("i2c init failed \n");
		return ret;
	}

    //sensor_connect_init(&i2c_config);
   	memcpy(&i2c_devices[pstViInfo->stSnsInfo.u32SnsId], &i2c_config, sizeof(i2c_dev_t));
    isp_i2c_init( pstViInfo->stSnsInfo.u32SnsId,
            &i2c_config,
            pstViInfo->stSnsInfo.u32BusId); 

    /* 2. Initialize MIPI connected to sensor */
	mipi_init(pstViInfo->stSnsInfo.u32MipiId);

	iChn.in.format       = pstViInfo->stSnsInfo.enFormat;
	iChn.in.res          = pstViInfo->stSnsInfo.enRes;
	iChn.crop_res_x		 = pstViInfo->stPipeInfo.cropResXoffst;
	iChn.crop_res_y		 = pstViInfo->stPipeInfo.cropResYoffst;
	iChn.out.format      = pstViInfo->stPipeInfo.enFormat;
	iChn.out.res         = pstViInfo->stPipeInfo.enRes;
	iChn.mipi_ch         = pstViInfo->stSnsInfo.u32MipiId;
	iChn.isp_ch          = pstViInfo->stPipeInfo.u32PipeId;
    iChn.isp_flt_enable  = pstViInfo->stPipeInfo.isp_flt_enable;
    iChn.denoise_level   = pstViInfo->stPipeInfo.denoise_level;
    iChn.sharpen_level   = pstViInfo->stPipeInfo.sharpen_level;
    iChn.isp_cproc_enable= pstViInfo->stPipeInfo.isp_cproc_enable;
    iChn.cproc_contrast  = pstViInfo->stPipeInfo.cproc_contrast;
    iChn.cproc_brightness= pstViInfo->stPipeInfo.cproc_brightness;
    iChn.cproc_saturation= pstViInfo->stPipeInfo.cproc_saturation;
    iChn.cproc_hue       = pstViInfo->stPipeInfo.cproc_hue;
    iChn.mipi_clk        = pstViInfo->stSnsInfo.u32MipiClk;
    /* 3. Initialize ISP connected to MIPI */
    if (pstViInfo->stLedInfo.enLightType == LED_LIGHT_TYPE_RGB)
    {
        isp_init(pstViInfo->stPipeInfo.u32PipeId, ISP_WORKMODE_RGB);
        vis_image_channel_set(&iChn, 
                    pstViInfo->stPipeInfo.bIspBypass,
                    pstViInfo->stSnsInfo.u32SnsId,
                    pstViInfo->stSnsInfo.u32BusId);
    }
    else if (pstViInfo->stLedInfo.enLightType == LED_LIGHT_TYPE_IR)
    {
        isp_init(pstViInfo->stPipeInfo.u32PipeId, ISP_WORKMODE_IR);       
        ir_image_channel_set(&iChn, 
                    pstViInfo->stPipeInfo.bIspBypass,
                    pstViInfo->stSnsInfo.u32SnsId,
                    pstViInfo->stSnsInfo.u32BusId);
    }

    /* 4. Initialize led */
    if (pstViInfo->stLedInfo.enLightType == LED_LIGHT_TYPE_IR)
    {
        //if (pstViInfo->stLedInfo.u32PortId != pstViInfo->stSnsInfo.u32PortId)
        {
            memset(&i2c_config,0,sizeof(i2c_config));
            i2c_config.port = pstViInfo->stLedInfo.u32PortId;
            i2c_config.config.dev_addr = pstViInfo->stLedInfo.u32BusId;
            i2c_config.universal = 1;   //for fullmask
            i2c_config.i2cNo = pstViInfo->stLedInfo.u32LedId;
            i2c_config.config.freq = I2C_BUS_BIT_RATES_400K;
            ret = hal_i2c_init(&i2c_config);
            if (ret < 0) 
            {
                printf("i2c init failed \n");
                return ret;
            }
            
            memcpy(&i2c_devices[pstViInfo->stLedInfo.u32LedId], &i2c_config, sizeof(i2c_dev_t));
        }
        
        ir_led_init(&i2c_config, pstViInfo->stLedInfo.u32BusId);
    }

    return 0;
}

/*  
 *  Video input deinitialization function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
int tsm_vi_stop(TSM_VI_INFO_S *pstViInfo)
{
    return 0;
}

/*  
 *  Video input standby function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
int tsm_vi_pause(TSM_VI_INFO_S *pstViInfo)
{
    if ((LED_LIGHT_TYPE_IR == pstViInfo->stLedInfo.enLightType) || (LED_LIGHT_TYPE_SPK == pstViInfo->stLedInfo.enLightType))
    {
        //printf("tsm_vi_pause \n");
        sensor_standby(pstViInfo->stPipeInfo.u32PipeId);
        isp_stop(pstViInfo->stPipeInfo.u32PipeId);

        if(g_ifv_flow_cfg.source_ir_left && g_ifv_flow_cfg.source_ir_right)
        {
            if(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].mipi_id == pstViInfo->stPipeInfo.u32PipeId)
            {
                //In order to keep the luma of the left and right images consistent
                aos_msleep(pstViInfo->stLedInfo.u32DelayTime);
                ir_led_power_off();
            }
        }
        else
        {
#if !defined(LED_STROBE)
            ir_led_power_off();
#endif
        }
    }
    else if (LED_LIGHT_TYPE_RGB == pstViInfo->stLedInfo.enLightType)
    {
        //ov9732_mode_config(); // Use sensor hal framework later. 
        isp_stop(pstViInfo->stPipeInfo.u32PipeId );
    }
    if ((TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].reconfig != 0)
        && ((TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].mipi_id == TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id)
        || (TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].mipi_id == TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].mipi_id))
        && (TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].mipi_id == pstViInfo->stPipeInfo.u32PipeId))
    {
        // tsm_vi_change_mipi_work_mode(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id, MIPI_WORK_MODE_RGB);
        if((mipiWorkStatusFlag & (1 << 2)) && (mipiWorkStatusFlag & 1)) //if work on ir /reg/recog and need change to rgb mode
        {
            aos_mutex_lock(&mipiWorkStatusMutex, AOS_WAIT_FOREVER);
			mipiWorkStatusFlag &= ~(1 << 2); // clear bit 2: don't need change to rgb mode
			mipiWorkStatusFlag |= 1 << 3; // set bit 3: need change to ir mode
			mipiWorkStatusFlag &= ~1; // clear bit 0 : not work on ir reg/recog mode
			mipiWorkStatusFlag |= 1 << 4; //set bit 4 :work on rgb mode
			tsm_vi_change_mipi_work_mode(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id, MIPI_WORK_MODE_RGB);
            isp_start(TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].isp_id);
			aos_mutex_unlock(&mipiWorkStatusMutex);
		}
		else if(!(mipiWorkStatusFlag & (1 << 2)) && (mipiWorkStatusFlag & 1)) //if work on ir reg/recog and don't need change to rgb mode
		{
            aos_mutex_lock(&mipiWorkStatusMutex, AOS_WAIT_FOREVER);
			mipiWorkStatusFlag &= ~1;// clear bit 0: not work on ir reg/recog mode
            aos_mutex_unlock(&mipiWorkStatusMutex);
		}
    }

    //aos_msleep(5);
    
    return 0;
}

/*  
 *  Video input resume from standby function
 *  @param[in] pstViInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */

int tsm_vi_resume(TSM_VI_INFO_S *pstViInfo)
{
    if ((TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].reconfig != 0)
        && (TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id == pstViInfo->stPipeInfo.u32PipeId)
        && ((TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].mipi_id == TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id)
        || (TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].mipi_id == TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].mipi_id)))
    {
        // tsm_vi_change_mipi_work_mode(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id, MIPI_WORK_MODE_IR);
        if (!(mipiWorkStatusFlag & (1 << 1)) && (mipiWorkStatusFlag & (1 << 4))) //if work on rgb and couldn't change mode,return
        {
            return -1;
        }
        else if ((mipiWorkStatusFlag & 1 << 1) && (mipiWorkStatusFlag & (1 << 4))) //if work on rgb and could change mode
        {
            aos_mutex_lock(&mipiWorkStatusMutex, AOS_WAIT_FOREVER);
            isp_stop(TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].isp_id);
			mipiWorkStatusFlag &= ~(1 << 3); // clear bit 3: don't need change to ir mode
			mipiWorkStatusFlag &= ~(1 << 1); // clear bit 1: couldn't change mipi work mode
			mipiWorkStatusFlag |= 1;  //work on ir reb/recog
			mipiWorkStatusFlag &= ~(1 << 4); //not work on rgb mode
			tsm_vi_change_mipi_work_mode(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id, MIPI_WORK_MODE_IR);
            aos_mutex_unlock(&mipiWorkStatusMutex);
        }
		else if (!(mipiWorkStatusFlag & 1)&& !(mipiWorkStatusFlag & (1 << 4))) //if not work mode on rgb and ir reb/recog
		{
            aos_mutex_lock(&mipiWorkStatusMutex, AOS_WAIT_FOREVER);
			mipiWorkStatusFlag |= 1; //set bit 0: work on ir reg/recog; go on
            aos_mutex_unlock(&mipiWorkStatusMutex);
		}
    }
    
    if (LED_LIGHT_TYPE_IR == pstViInfo->stLedInfo.enLightType)
    {
        isp_reset(pstViInfo->stPipeInfo.u32PipeId);
        if (g_ifv_flow_cfg.source_spk) {
        	cfg_sensor_mi_buffer(0);
        }
		if (g_ifv_flow_cfg.source_ir_left && g_ifv_flow_cfg.source_ir_right){
		}
		else{
			ir_led_power_off();
		}
		
		//printf("on\n");
        ir_led_power_on();	
//		if (g_ifv_flow_cfg.source_ir_left && g_ifv_flow_cfg.source_ir_right)
//        	aos_msleep(33);
		//mipi_restart(pstViInfo->stPipeInfo.u32PipeId);
		//isp_reset(pstViInfo->stPipeInfo.u32PipeId);
        CamIrAAACntReinit(pstViInfo->stPipeInfo.u32PipeId);
        isp_start(pstViInfo->stPipeInfo.u32PipeId);
		sensor_stream(pstViInfo->stPipeInfo.u32PipeId);
    }
    else if (LED_LIGHT_TYPE_SPK == pstViInfo->stLedInfo.enLightType)
    {	
        isp_reset(pstViInfo->stPipeInfo.u32PipeId);	
        if (g_ifv_flow_cfg.source_spk)
        	cfg_sensor_mi_buffer(1);
        ir_led_power_off();
        if(g_scenario_type == SENSOR_EXPO_OUTDOOR){
            //printf("spk current: 1000mA \n");
            speckle_power_on_outside();
        }    
        else if(g_scenario_type == SENSOR_EXPO_INDOOR){
            //printf("spk current: 500mA \n");
            speckle_power_on();
        }
        //mipi_restart(pstViInfo->stPipeInfo.u32PipeId);   
		//isp_reset(pstViInfo->stPipeInfo.u32PipeId);
#if 1//defined(IR_SENSOR_ISP_BYPASS)
		//isp_reg_write(pstViInfo->stPipeInfo.u32PipeId, MI_INIT, 0x10);
        isp_start(pstViInfo->stPipeInfo.u32PipeId);
#endif 		
        sensor_stream(pstViInfo->stPipeInfo.u32PipeId);
    }
    else if (LED_LIGHT_TYPE_RGB == pstViInfo->stLedInfo.enLightType)
    {
	    isp_start(pstViInfo->stPipeInfo.u32PipeId );
        //ov9732_mode_config(); // Use sensor hal framework later. 
    }
    
    return 0;
}

static dpu_layer_cfg_t dpu_layer_cfg = {0};

/*  
 *  Video output initialization function
 *  @param[in] pstVoInfo    pointer to the configuration information.
 *  @return    0 success other failed
 */
// int tsm_vo_start(TSM_VO_INFO_S *pstVoInfo)
// {
//     void *baseaddr;

//     /* 1. Initialize LCD */
//     LCD_GPIO_Init2p8(); 
//     LCD_Init2p8();
    
//     /* 2. Initialize DPU */
//     dpu_handle = csi_dpu_initialize(0, 0, 0);
//     *(int *)0xffd64004 = 0x407;   //reduce clock

//     dpu_cfg_display_timing_t timing;
//     //2.8 lcd
// 	timing.resolution_x  = pstVoInfo->stVoIntfInfo.stRes.u32Width;
// 	timing.resolution_y  = pstVoInfo->stVoIntfInfo.stRes.u32Height;
// 	timing.hsync_width	 = 10;	   
// 	timing.vsync_width	 = 4;	  
// 	timing.front_porch_x = 38;	  
// 	timing.front_porch_y = 8;	 
// 	timing.back_porch_x  = 10;	  
// 	timing.back_porch_y  = 4;	 

//     dpu_cfg_display_signal_polarity_t polarity;
//     polarity.hsync       = DPU_SIGNAL_ACTIVE_LOW;
//     polarity.vsync       = DPU_SIGNAL_ACTIVE_LOW;
//     polarity.deta_enable = DPU_SIGNAL_ACTIVE_HIGH;
//     polarity.pixel_clock = DPU_SIGNAL_ACTIVE_HIGH;

//     csi_dpu_config_display(dpu_handle, &timing, &polarity);
    
// 	baseaddr = aos_malloc(pstVoInfo->stVoIntfInfo.stRes.u32Width * 
//                     pstVoInfo->stVoIntfInfo.stRes.u32Height * 2);
//     if(baseaddr == NULL)
//     {
//         printf("mem error!\n");
// 		return -1;
//     }

//     dpu_layer_cfg.startx      = 0;
//     dpu_layer_cfg.starty      = 0;
//     dpu_layer_cfg.resx        = pstVoInfo->stLayerInfo.stDispRect.u32Width;
//     dpu_layer_cfg.resy        = pstVoInfo->stLayerInfo.stDispRect.u32Height;
//     dpu_layer_cfg.sizex       = pstVoInfo->stLayerInfo.stDispRect.u32Width;
//     dpu_layer_cfg.sizey       = pstVoInfo->stLayerInfo.stDispRect.u32Height;
//     dpu_layer_cfg.colormode   = pstVoInfo->stLayerInfo.enFormat;
//     dpu_layer_cfg.blendmode   = pstVoInfo->stLayerInfo.enBlend;
//     dpu_layer_cfg.alpha       = 0;
//     dpu_layer_cfg.baseaddr    = baseaddr;
    
// 	csi_dpu_layer_set_config(dpu_handle, 0, &dpu_layer_cfg);
// 	csi_dpu_layer_enable(dpu_handle, 0, 1);
// 	csi_dpu_start(dpu_handle);

//     /* 3. Initialize GUI */
//     lv_port_disp_init();

//     return 0;
// }

/*  
 *  Get dpu display memory address function
 *  @return    dpu display memory address
 */
void *tsm_vo_get_disp_memory()
{
    return dpu_layer_cfg.baseaddr;
}

/*  
 *  Mipi work mode change function
 *  @param[in] mipi_ID: ID of changed mipi.
 *  @param[in] mipiWorkMode_Flag: mipi work mode.
 *  @return    0 success other failed
 */
int tsm_vi_change_mipi_work_mode(uint8_t mipi_ID, uint8_t mipiWorkMode_Flag)
{
    int ret = 0;
    if (mipiWorkModeFlag == mipiWorkMode_Flag)
    {
        return ret;
    }
    if (mipiWorkMode_Flag == MIPI_WORK_MODE_RGB)
    {
        mipiWorkModeFlag = MIPI_WORK_MODE_RGB;
        aos_msleep(10); // it is necessary
        ifv_isp_task_delete();
        ifv_ISP_ir_cis_task_delete();
        mipi_restart(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id);
        mipi_restart(TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].isp_id);
        isp_reset(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id);
        isp_reset(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].isp_id);
        isp_unInit(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id);
        isp_unInit(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].isp_id);
        CamEngineRelease(0);
        CamEngineRelease(1);
        sensor_reconfig(mipi_ID, mipiWorkMode_Flag);
        ifv_isp_task_creat();
        ifv_ISP_ir_cis_task_creat();
        return ret;
    }
    else if (mipiWorkMode_Flag == MIPI_WORK_MODE_IR)
    {
        float ir_exp = 0;
        mipiWorkModeFlag = MIPI_WORK_MODE_IR;
        ifv_isp_task_delete();
        ifv_ISP_ir_cis_task_delete();
        mipi_restart(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id);
        mipi_restart(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].isp_id);
        isp_reset(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id);
        isp_reset(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].isp_id);
        isp_unInit(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id);
        isp_unInit(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].isp_id);
        CamEngineRelease(0);
        CamEngineRelease(1);
        ifv_getIspExpValueKv(&ir_exp);
        sensor_reconfig(mipi_ID, mipiWorkMode_Flag);
        ifv_isp_task_creat();
        ifv_ISP_ir_cis_task_creat();
        qw_isp_ae_lock_judge_set(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id, true);
        qw_isp_ae_lock_judge_set(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].isp_id, true);
        qw_isp_ae_enable_set(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].isp_id, true);
        qw_isp_ae_enable_set(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_RIGHT].isp_id, true);
        g_face_ae_info.loop_counter = 2;
        return ret;
    }
    return 0;
}

int tsm_open_peephole(void)
{
#if defined(AOS_BOARD_TX5103C_LOCK_DUALIR_PEEPHOLE_ABHAM)
    if (mipiWorkStatusFlag & 1) //if on ir reg/recog,return
    {
        aos_mutex_lock(&mipiWorkStatusMutex, AOS_WAIT_FOREVER);
        mipiWorkStatusFlag |= 1 << 2; //set bit 2: need change to rgb mode
        aos_mutex_unlock(&mipiWorkStatusMutex);
		return 1;
    }
    else
    {
        aos_mutex_lock(&mipiWorkStatusMutex, AOS_WAIT_FOREVER);
        mipiWorkStatusFlag |= 1 << 3; //set bit 3 ,need change to ir
        mipiWorkStatusFlag |= 1 << 4; //set bit 4 , work on rgb mode
        tsm_vi_change_mipi_work_mode(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id, MIPI_WORK_MODE_RGB);
		isp_start(TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].isp_id);
        aos_mutex_unlock(&mipiWorkStatusMutex);
    }
#else
    isp_start(TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].isp_id);
#endif
	return 0;
}

int tsm_close_peephole(void)
{
#if defined(AOS_BOARD_TX5103C_LOCK_DUALIR_PEEPHOLE_ABHAM)
    if ((mipiWorkStatusFlag & (1 << 3)) && mipiWorkStatusFlag &(1 << 4)) // if work on rgb mode ande need change to ir mode
    {
        aos_mutex_lock(&mipiWorkStatusMutex, AOS_WAIT_FOREVER);
        isp_stop(TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].isp_id);
        tsm_vi_change_mipi_work_mode(TX510_sensor_defConfig[IFV_SENSOR_TYPE_IR_LEFT].mipi_id, MIPI_WORK_MODE_IR);	
        mipiWorkStatusFlag &= ~(1 << 3); //clear bit 3:  don't need change to ir
        mipiWorkStatusFlag &= ~(1 << 4); //clear bit 4: not work on rgb
        aos_mutex_unlock(&mipiWorkStatusMutex);
    }
#else
    isp_stop(TX510_sensor_defConfig[IFV_SENSOR_TYPE_RGB].isp_id);
#endif
	return 0;
}

